import 'package:appflutterhomeschool/api/api_service.dart';
import 'package:appflutterhomeschool/api/dio_utils.dart';
import 'package:appflutterhomeschool/common/common.dart';
import 'package:appflutterhomeschool/model/form_switch_model.dart';
import 'package:appflutterhomeschool/model/student_information_model.dart';
import 'package:appflutterhomeschool/provider/provider_widget.dart';
import 'package:appflutterhomeschool/res/styles.dart';
import 'package:appflutterhomeschool/route/application.dart';
import 'package:appflutterhomeschool/route/routes.dart';
import 'package:appflutterhomeschool/ui/dialogs/prompt_dialog_widgets.dart';
import 'package:appflutterhomeschool/ui/widgets/button_widget.dart';
import 'package:appflutterhomeschool/ui/widgets/my_app_bar.dart';
import 'package:appflutterhomeschool/ui/widgets/user/student_widget.dart';
import 'package:appflutterhomeschool/ui/widgets/widgets.dart';
import 'package:appflutterhomeschool/view_model/my/student_information_view_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';

///用户： ninetailedfox
///时间： 2020/6/26
///邮箱： 1037438704@qq.com
///功能： 学生信息
class StudentInformationPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ProviderWidget<StudentInformationViewModel>(
      model: StudentInformationViewModel(context),
      onModelReady: (value) => value.initView(),
      builder: (context, viewModel, child) {
        return Scaffold(
          backgroundColor: Colors.grey[100],
          appBar: MyAppBar2(title: viewModel.identity ? "班级信息" : "学生信息"),
          body: SingleChildScrollView(
            child: Column(
              children: <Widget>[
                ListView.builder(
                  physics: NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                  itemCount: viewModel.studentList == null
                      ? 0
                      : viewModel.studentList.length ?? 0,
                  itemBuilder: (context, index) {
                    StudentInformationModel studenModel =
                        viewModel.studentList[index];
                    return Padding(
                      padding: EdgeInsets.only(
                          left: ScreenUtil().setWidth(35),
                          top: ScreenUtil().setWidth(35),
                          right: ScreenUtil().setWidth(35)),
                      child: Material(
                        color: Colors.white,
                        elevation: 4.0,
                        borderRadius: BorderRadius.circular(10),
                        child: InkWell(
                          borderRadius: BorderRadius.circular(10),
                          child: Container(
                            padding: EdgeInsets.only(
                                top: ScreenUtil().setWidth(51),
                                left: ScreenUtil().setWidth(35),
                                bottom: ScreenUtil().setWidth(35),
                                right: ScreenUtil().setWidth(35)),
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.start,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                viewModel.identity
                                    ? SwitchRowWidget(
                                        imageUrl: studenModel?.imageUrl,
                                        name: studenModel.className,
                                      )
                                    : Text(studenModel.userName,
                                        style: TextStyles.textDark43),
                                SizedBox(height: ScreenUtil().setWidth(53)),
                                Row(
                                  children: <Widget>[
                                    RowWidgetLab(
                                      visible: studenModel.school != "",
                                      textContent: "${studenModel.school}",
                                      imageUrl: "wode_xuexiao",
                                    ),
                                    RowWidgetLab(
                                        visible: studenModel.className != "",
                                        textContent:
                                            studenModel.className ?? "",
                                        imageUrl: "wode_nianji"),
                                    RowWidgetLab(
                                      visible: studenModel.code != "",
                                      textContent: "${studenModel.code}",
                                      imageUrl: "bianma",
                                    ),
                                    RowWidgetLab(
                                      visible: !viewModel.identity,
                                      textContent: "一班",
                                      imageUrl: "wode_banji",
                                    ),
                                  ],
                                ),
                                Container(
                                  padding: EdgeInsets.only(
                                      top: ScreenUtil().setWidth(47)),
                                  child: Row(
                                    crossAxisAlignment: CrossAxisAlignment.end,
                                    mainAxisAlignment: MainAxisAlignment.end,
                                    children: [
                                      RowWidgetLab(
                                        visible: viewModel.identity,
                                        crossAxisAlignment:
                                            CrossAxisAlignment.center,
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        rightPadding: 70,
                                        textContent: "复制代码",
                                        imageUrl: "copy",
                                        onTap: () {
                                          Clipboard.setData(ClipboardData(
                                              text: studenModel.code));
                                          Fluttertoast.showToast(msg: "复制成功");
                                        },
                                      ),
                                      RowWidgetLab(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.center,
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        rightPadding: 0,
                                        textContent: "删除",
                                        imageUrl: "wode_shanchu",
                                        onTap: () =>
                                            viewModel.deleteOnTap(index),
                                      )
                                    ],
                                  ),
                                )
                              ],
                            ),
                          ),
                        ),
                      ),
                    );
                  },
                ),
                Visibility(
                  visible: viewModel.identity,
                  child: Container(
                    margin: EdgeInsets.fromLTRB(
                        ScreenUtil().setWidth(35),
                        ScreenUtil().setWidth(35),
                        ScreenUtil().setWidth(35),
                        0),
                    child: ButtonWidget().filletButtonWidget(
                        buttonName: "新建班级",
                        onPressed: () {
                          Application().navigatePushToPage(
                              context,
                              Routes.newClassPage,
                              {}).then((value) => viewModel.initView());
                        }),
                  ),
                ),
                Container(
                  margin: EdgeInsets.fromLTRB(
                      ScreenUtil().setWidth(35),
                      ScreenUtil().setWidth(35),
                      ScreenUtil().setWidth(35),
                      ScreenUtil().setWidth(200)),
                  child: ButtonWidget().outlineButtonWidget(
                      textStyle: TextStyles.textBlue37,
                      buttonName: viewModel.identity ? "加入班级" : "添加学生信息",
                      borderSideColor: Colors.blue,
                      widthDouble: double.maxFinite,
                      heightDouble: ScreenUtil().setWidth(115),
                      onPressed: () {
                        TextEditingController textEditing =
                            TextEditingController();
                        //第一步添加学生代码
                        PromptDialogWidgets(context).showEditDialog(
                            textEditing1: textEditing,
                            identity: viewModel.identity,
                            onTap: () {
                              if (textEditing.text == "") {
                                Fluttertoast.showToast(msg: "请填写班级代码！");
                                return;
                              }
                              //需要判断是老师还是家长
                              PromptDialogWidgets(context).showButtomDialog(
                                  gridList: viewModel.gridList,
                                  identity: viewModel.identity,
                                  onTap: (ValueModel valueModel) {
                                    if (viewModel.identity) {
                                      DioUtils.getInstance()
                                          .post(ApiService.teacherJoinClass, {
                                        "token": Constant.getToken(),
                                        "cls_code": textEditing.text,
                                        "subject": valueModel.relationship,
                                      }, (successCallBack) {
                                        Fluttertoast.showToast(msg: "加入成功");
                                        Navigator.pop(context);
                                      }, (errorCallBack) {});
                                    } else {
                                      //班级代码
                                      DioUtils.getInstance()
                                          .post(ApiService.addStudent, {
                                        "token": Constant.getToken(),
                                        "cls_code": textEditing.text,
                                        "name": valueModel.name,
                                        "relation": valueModel.relationship,
                                      }, (successCallBack) {
                                        Fluttertoast.showToast(
                                            msg: successCallBack["message"]);
                                        viewModel.initView();
                                      }, (errorCallBack) {});
                                    }
                                  });
                            });
                      }),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
